﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PhatPhucLoc.dao;

namespace PhatPhucLoc.view
{
    public partial class FormThemHangHoa : Form
    {
        private FormHangHoa formHangHoa = null;
        private HangHoaModel hangHoaModel ;
        public FormThemHangHoa()
        {
            InitializeComponent();
            load();
        }

        public FormThemHangHoa(ref FormHangHoa _formHangHoa)
        {
            InitializeComponent();
            formHangHoa = _formHangHoa;
            load();
        }

        public FormThemHangHoa(HangHoaModel hangHoaModel,ref FormHangHoa _formHangHoa)
        {
            InitializeComponent();
            formHangHoa = _formHangHoa;
            this.hangHoaModel = hangHoaModel;
            txtTen.Text = hangHoaModel.Ten;
            txtMaHang.Text = hangHoaModel.MaHang;
            txtGia.Text = hangHoaModel.GiaNhap;
            txtGhiChu.Text = hangHoaModel.GhiChu;
            load();
            cbbNhomHang.SelectedValue = hangHoaModel.MaNhomHang;
        }

        public void load()
        {
                cbbNhomHang.DisplayMember = "ten";
                cbbNhomHang.ValueMember = "id";
                cbbNhomHang.DataSource = NhomHangDAO.GetAll();
        }

        private void btnChapNhan_Click(object sender, EventArgs e)
        {
            if (txtTen.Text.Equals(""))
            {
                MessageBox.Show(" Tên  Hàng Không Được Trống");
                txtTen.Focus();
            }
            else
            {
                if (hangHoaModel == null)
                {
                    hangHoaModel = new HangHoaModel();
                }
                hangHoaModel.GhiChu = txtGhiChu.Text;
                hangHoaModel.Ten = txtTen.Text;
                hangHoaModel.GiaNhap = txtGia.Text;
                hangHoaModel.MaNhomHang = cbbNhomHang.SelectedValue.ToString();
                hangHoaModel.MaHang = txtMaHang.Text;

                // new
                if (hangHoaModel.Id == null || hangHoaModel.Id == "")
                {
                    if (HangHoaDAO.Insert(hangHoaModel))
                    {
                        MessageBox.Show("Thêm Hàng Hóa thành công");
                        if (formHangHoa != null)
                        {
                            formHangHoa.FormHangHoaLoad();
                        }
                        this.Dispose();
                    }
                }
                // update
                else
                {
                    if (HangHoaDAO.Update(hangHoaModel))
                    {
                        MessageBox.Show("Cập nhật Hàng Hóa  thành công");
                        formHangHoa.FormHangHoaLoad();
                        this.Dispose();
                    }
                }
            }
        }


    }
}
